<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Introduction - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.codegenerator.introduction.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.codegenerator.introduction.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.codegenerator.html">Zend_CodeGenerator</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.codegenerator.html">Zend_CodeGenerator</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.codegenerator.examples.html">Zend_CodeGenerator Examples</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.codegenerator.introduction" class="section"><div class="info"><h1 class="title">Introduction</h1></div>
    

    <p class="para">
        <span class="classname">Zend_CodeGenerator</span> provides facilities to generate
        arbitrary code using an object oriented interface, both to create new
        code as well as to update existing code. While the current
        implementation is limited to generating <acronym class="acronym">PHP</acronym> code, you can easily extend
        the base class in order to provide code generation for other tasks:
        JavaScript, configuration files, apache vhosts, etc.
    </p>

    <div class="section" id="zend.codegenerator.introduction.theory"><div class="info"><h1 class="title">Theory of Operation</h1></div>
        

        <p class="para">
            In the most typical use case, you will simply instantiate a code
            generator class and either pass it the appropriate configuration or
            configure it after instantiation. To generate the code, you will
            simply echo the object or call its  <span class="methodname">generate()</span> method.
        </p>

        <pre class="programlisting brush: php">
// Passing configuration to the constructor:
$file = new Zend_CodeGenerator_Php_File(array(
    &#039;classes&#039; =&gt; array(
        new Zend_CodeGenerator_Php_Class(array(
            &#039;name&#039;    =&gt; &#039;World&#039;,
            &#039;methods&#039; =&gt; array(
                new Zend_CodeGenerator_Php_Method(array(
                    &#039;name&#039; =&gt; &#039;hello&#039;,
                    &#039;body&#039; =&gt; &#039;echo \&#039;Hello world!\&#039;;&#039;,
                )),
            ),
        )),
    )
));

// Configuring after instantiation
$method = new Zend_CodeGenerator_Php_Method();
$method-&gt;setName(&#039;hello&#039;)
       -&gt;setBody(&#039;echo \&#039;Hello world!\&#039;;&#039;);

$class = new Zend_CodeGenerator_Php_Class();
$class-&gt;setName(&#039;World&#039;)
      -&gt;setMethod($method);

$file = new Zend_CodeGenerator_Php_File();
$file-&gt;setClass($class);

// Render the generated file
echo $file;

// or write it to a file:
file_put_contents(&#039;World.php&#039;, $file-&gt;generate());
</pre>


        <p class="para">
            Both of the above samples will render the same result:
        </p>

        <pre class="programlisting brush: php">
&lt;?php

class World
{

    public function hello()
    {
        echo &#039;Hello world!&#039;;
    }

}
</pre>


        <p class="para">
            Another common use case is to update existing code -- for instance,
            to add a method to a class. In such a case, you must first inspect
            the existing code using reflection, and then add your new method.
            <span class="classname">Zend_CodeGenerator</span> makes this trivially simple, by
            leveraging <a href="zend.reflection.html" class="link">Zend_Reflection</a>.
        </p>

        <p class="para">
            As an example, let&#039;s say we&#039;ve saved the above to the file
            &quot;<var class="filename">World.php</var>&quot;, and have already included it. We could then do the
            following:
        </p>

        <pre class="programlisting brush: php">
$class = Zend_CodeGenerator_Php_Class::fromReflection(
    new Zend_Reflection_Class(&#039;World&#039;)
);

$method = new Zend_CodeGenerator_Php_Method();
$method-&gt;setName(&#039;mrMcFeeley&#039;)
       -&gt;setBody(&#039;echo \&#039;Hello, Mr. McFeeley!\&#039;;&#039;);
$class-&gt;setMethod($method);

$file = new Zend_CodeGenerator_Php_File();
$file-&gt;setClass($class);

// Render the generated file
echo $file;

// Or, better yet, write it back to the original file:
file_put_contents(&#039;World.php&#039;, $file-&gt;generate());
</pre>


        <p class="para">
            The resulting class file will now look like this:
        </p>

        <pre class="programlisting brush: php">
&lt;?php

class World
{

    public function hello()
    {
        echo &#039;Hello world!&#039;;
    }

    public function mrMcFeeley()
    {
        echo &#039;Hellow Mr. McFeeley!&#039;;
    }

}
</pre>

    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.codegenerator.html">Zend_CodeGenerator</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.codegenerator.html">Zend_CodeGenerator</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.codegenerator.examples.html">Zend_CodeGenerator Examples</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="reference.html">Refer&ecirc;ncia do Zend Framework</a></li>
  <li class="header up"><a href="zend.codegenerator.html">Zend_CodeGenerator</a></li>
  <li class="active"><a href="zend.codegenerator.introduction.html">Introduction</a></li>
  <li><a href="zend.codegenerator.examples.html">Zend_CodeGenerator Examples</a></li>
  <li><a href="zend.codegenerator.reference.html">Zend_CodeGenerator Reference</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>